using System;
using System.Collections;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;

using WHC.Pager.Entity;
using Microsoft.Practices.EnterpriseLibrary.Data;
using WHC.Hotel.Entity;
using WHC.Hotel.IDAL;
using WHC.OrderWater.Commons;

namespace WHC.Hotel.DAL
{
	/// <summary>
	/// PurchaseHeader 的摘要说明。
	/// </summary>
	public class PurchaseHeader : BaseDAL<PurchaseHeaderInfo>, IPurchaseHeader
	{
		#region 对象实例及构造函数

		public static PurchaseHeader Instance
		{
			get
			{
				return new PurchaseHeader();
			}
		}
		public PurchaseHeader() : base("TB_PurchaseHeader","ID")
		{
		}

		#endregion

		/// <summary>
		/// 将DataReader的属性值转化为实体类的属性值，返回实体类
		/// </summary>
		/// <param name="dr">有效的DataReader对象</param>
		/// <returns>实体类对象</returns>
		protected override PurchaseHeaderInfo DataReaderToEntity(IDataReader dataReader)
		{
			PurchaseHeaderInfo purchaseHeaderInfo = new PurchaseHeaderInfo();
			SmartDataReader reader = new SmartDataReader(dataReader);
			
			purchaseHeaderInfo.ID = reader.GetInt32("ID");
			purchaseHeaderInfo.HandNo = reader.GetString("HandNo");
			purchaseHeaderInfo.OperationType = reader.GetString("OperationType");
			purchaseHeaderInfo.Manufacture = reader.GetString("Manufacture");
			purchaseHeaderInfo.WareHouse = reader.GetString("WareHouse");
			purchaseHeaderInfo.Yinfu = reader.GetDecimal("Yinfu");
			purchaseHeaderInfo.Paid = reader.GetDecimal("Paid");
			purchaseHeaderInfo.YinShou = reader.GetDecimal("YinShou");
			purchaseHeaderInfo.Received = reader.GetDecimal("Received");
			purchaseHeaderInfo.Note = reader.GetString("Note");
			purchaseHeaderInfo.CreateDate = reader.GetDateTime("CreateDate");
			purchaseHeaderInfo.Creator = reader.GetString("Creator");
			
			return purchaseHeaderInfo;
		}

		/// <summary>
		/// 将实体对象的属性值转化为Hashtable对应的键值
		/// </summary>
		/// <param name="obj">有效的实体对象</param>
		/// <returns>包含键值映射的Hashtable</returns>
        protected override Hashtable GetHashByEntity(PurchaseHeaderInfo obj)
		{
		    PurchaseHeaderInfo info = obj as PurchaseHeaderInfo;
			Hashtable hash = new Hashtable(); 
			
 			hash.Add("HandNo", info.HandNo);
 			hash.Add("OperationType", info.OperationType);
 			hash.Add("Manufacture", info.Manufacture);
 			hash.Add("WareHouse", info.WareHouse);
 			hash.Add("Yinfu", info.Yinfu);
 			hash.Add("Paid", info.Paid);
 			hash.Add("YinShou", info.YinShou);
 			hash.Add("Received", info.Received);
 			hash.Add("Note", info.Note);
 			hash.Add("CreateDate", info.CreateDate);
 			hash.Add("Creator", info.Creator);
 				
			return hash;
		}

        /// <summary>
        /// 获取采购支出费用总额
        /// </summary>
        /// <param name="dtStart"></param>
        /// <param name="dtEnd"></param>
        /// <returns></returns>
        public Dictionary<string, decimal> GetFeeAmount(DateTime dtStart, DateTime dtEnd, string wareHouse)
        {
            Dictionary<string, decimal> dict = new Dictionary<string, decimal>();

            string condition = string.Format("where CreateDate >='{0}' and CreateDate <= '{1}' ", dtStart, dtEnd);
            if (!string.IsNullOrEmpty(wareHouse))
            {
                condition += string.Format(" AND WareHouse ='{0}'  ", wareHouse);
            }
            string sql = string.Format(@"
select WareHouse, ISNULL(-Sum(Paid-Received),0) Amount 
from TB_PurchaseHeader {0}  group by WareHouse ", condition);

            Database db = DatabaseFactory.CreateDatabase();
            DbCommand command = db.GetSqlStringCommand(sql);
            using (IDataReader dr = db.ExecuteReader(command))
            {
                SmartDataReader reader = new SmartDataReader(dr);
                while (reader.Read())
                {
                    string key = string.Format("{0}采购支出", reader.GetString("WareHouse"));
                    dict.Add(key, reader.GetDecimal("Amount"));
                }
            }
            
            return dict;
        }

        
    }
}